package cn.edu.cuit.elena.common;

import java.util.Map;

/**
 * Configuration is based on <code>HashMap</code>, the <code>instance</code> method is created by
 * the default system.ini
 * <p>
 * file. You can get particular Configuration object from file via <code>create</code> method.
 * */
public class Configuration
    implements Configurable
{
    private Map<String, String> properties;
    private static Configuration configuration;

    private Configuration(String filePath)
    {
        properties = FileHandler.loadPropertyFile( filePath );
    }

    public static Configuration instance()
    {
        if( configuration == null )
        {
            configuration = new Configuration( SackConstants.SYSTEM_CONFIGURATION );
        }

        return configuration;
    }

    public static Configuration create( String path )
    {
        return new Configuration( path );
    }

    public String getProperty( String property )
    {
        return properties.get( property );
    }

    public Map<String, String> getProperties()
    {
        return properties;
    }

    public void append( Configurable configurable )
    {
        for( String key : configurable.getProperties().keySet() )
        {
            if( properties.containsKey( key ) )
            {
                throw new DuplicateException(key + "has been contained.");
            }
        }
        
        properties.putAll( configurable.getProperties() );
    }
}
